package gew.servlets;

import gew.html.HtmlTemplates;
import gew.js.JsTemplates;
import gew.services.DimensionValueService;
import gew.system.Constants;
import gew.system.Labels;
import gew.system.Parameters;
import gew.system.RequestParameter;
import gew.utils.SqlUtils;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "dynamicJs", urlPatterns = {"/dynamicJs"})
public class DynamicJs extends HttpServlet {
    
    @EJB
    private DimensionValueService dvs;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestParameter rp = new RequestParameter(request, dvs);
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/javascript");
        response.setCharacterEncoding("UTF-8");
        switch (rp.getSection()) {
            case Constants.SECTION_TRADE_JS: {
                response.getWriter().write(getTradeJs(rp));
            }
            break;
            default:
                response.getWriter().write("");
        }
    }

    private String getTradeJs(RequestParameter rp) {
        String r = JsTemplates.getTemplate(JsTemplates.TRADE);
        String balloonTemplate
                = HtmlTemplates.getTemplate(HtmlTemplates.INDICATORS_GRAPH)
                .replace("@reporterTitle", Labels.get_UI_TITLE_REPORTER(rp.getLocale()))
                .replace("@tradeFlowTitle", Labels.get_UI_TITLE_TRADE_FLOW(rp.getLocale()))
                .replace("@partnerTitle", Labels.get_UI_TITLE_PARTNER(rp.getLocale()))
                .replace("@periodTitle", Labels.get_UI_TITLE_PERIOD(rp.getLocale()))
                .replace("@tradeValueTitle", Labels.get_UI_TITLE_TRADE_VALUE(rp.getLocale()))
                .replace("@shareOfTotalTitle", Labels.get_UI_TITLE_TRADE_SHARE_TOTAL(rp.getLocale()))
                .replace("@rankTitle", Labels.get_UI_TITLE_TRADE_RANK(rp.getLocale()))
                .replace("@commodityTitle", Labels.get_UI_TITLE_COMMODITY(rp.getLocale()));
        String urlTopNPartners = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.TRADE_FLOW_IDS + "]")
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.COMMODITY_IDS + "]")
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.PARTNER_RANK + "]")
                .concat(SqlUtils.QUESTION)
                .concat(Parameters.POSTFIX_TRADE_TOP_PARTNERS_DATA);
        String urlSunburst = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.TRADE_FLOW_IDS + "]")
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.PARTNER_IDS + "]")
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.PERIOD_IDS + "]")
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.QUESTION)
                .concat(Parameters.POSTFIX_TRADE_SUNBURST_DATA);
        r = r.replace("@urlSunburst@", urlSunburst);
        r = r.replace("@urlTopPartners@", urlTopNPartners);
        r = r.replace("@balloonTemplate@", balloonTemplate);
        r = r.replace("@sunburstId@", Parameters.POSTFIX_TRADE_SUNBURST_DATA);
        r = r.replace("@topNPartnersId@", Parameters.POSTFIX_TRADE_TOP_PARTNERS_DATA);
        r = r.replace("@pTradeFlow@", Parameters.TRADE_FLOW_IDS);
        r = r.replace("@pCommodity@", Parameters.COMMODITY_IDS);
        r = r.replace("@pPartner@", Parameters.PARTNER_IDS);
        r = r.replace("@pPeriod@", Parameters.PERIOD_IDS);
        r = r.replace("@pPartnerRank@", Parameters.PARTNER_RANK);
        
        String urlTopPartnersClickStart = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH);
        String urlTopPartnersClickVar = rp.getPartnerUrlCode();
        String urlTopPartnersClickEnd = SqlUtils.SLASH
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.SLASH)
                .concat("[" + Parameters.PARTNER_RANK + "]");        
        r = r.replace("@urlTopPartnersClickStart@", urlTopPartnersClickStart);
        r = r.replace("@urlTopPartnersClickVar@", urlTopPartnersClickVar);
        r = r.replace("@urlTopPartnersClickEnd@", urlTopPartnersClickEnd);
        
        String urlSunburstClickStart = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH);
        String urlSunburstClickVar = rp.getCommodityUrlCode();
        String urlSunburstClickEnd = SqlUtils.SLASH
                .concat("[" + Parameters.PARTNER_RANK + "]");        
        r = r.replace("@urlSunburstClickStart@", urlSunburstClickStart);
        r = r.replace("@urlSunburstClickVar@", urlSunburstClickVar);
        r = r.replace("@urlSunburstClickEnd@", urlSunburstClickEnd);
        
        return r;
    }
}
